{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8f39fb8a",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/observability/Deepeval.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b499947e",
   "metadata": {},
   "source": [
    "# DeepEval: Evaluation and Observability for LlamaIndex\n",
    "\n",
    "DeepEval (by [Confident AI](https://documentation.confident-ai.com/docs)) now integrates with LlamaIndex, giving you end-to-end visibility and evaluation tools for your LlamaIndex agents."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99b6fd42",
   "metadata": {},
   "source": [
    "## Quickstart\n",
    "Install the following packages:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ffaa5145",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -U deepeval llama-index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ceafaac",
   "metadata": {},
   "source": [
    "Login with your [Confident API key](https://confident-ai.com/) and configure DeepEval as instrument LlamaIndex:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3e8ff75",
   "metadata": {},
   "outputs": [],
   "source": [
    "import llama_index.core.instrumentation as instrument\n",
    "\n",
    "import deepeval\n",
    "from deepeval.integrations.llama_index import instrument_llama_index\n",
    "\n",
    "deepeval.login(\"<your-confident-api-key>\")\n",
    "\n",
    "instrument_llama_index(instrument.get_dispatcher())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0660758d",
   "metadata": {},
   "source": [
    "### Example Agent\n",
    "\n",
    "⚠️ **Note**: DeepEval may not work reliably in Jupyter notebooks due to event loop conflicts. It is recommended to run examples in a standalone Python script instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7133e916",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "import asyncio\n",
    "\n",
    "from llama_index.llms.openai import OpenAI\n",
    "import llama_index.core.instrumentation as instrument\n",
    "from llama_index.core.agent.workflow import FunctionAgent\n",
    "\n",
    "import deepeval\n",
    "from deepeval.integrations.llama_index import instrument_llama_index\n",
    "\n",
    "# Don't forget to setup tracing\n",
    "deepeval.login(\"<your-confident-api-key>\")\n",
    "\n",
    "# Instrument LlamaIndex\n",
    "instrument_llama_index(instrument.get_dispatcher())\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"<your-openai-api-key>\"\n",
    "\n",
    "\n",
    "def multiply(a: float, b: float) -> float:\n",
    "    \"\"\"Useful for multiplying two numbers.\"\"\"\n",
    "    return a * b\n",
    "\n",
    "\n",
    "agent = FunctionAgent(\n",
    "    tools=[multiply],\n",
    "    llm=OpenAI(model=\"gpt-4o-mini\"),\n",
    "    system_prompt=\"You are a helpful assistant that can perform calculations.\",\n",
    ")\n",
    "\n",
    "\n",
    "async def main():\n",
    "    response = await agent.run(\"What's 7 * 8?\")\n",
    "    print(response)\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    asyncio.run(main())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b56dd3b",
   "metadata": {},
   "source": [
    "You can directly view the traces in the **Observatory** by clicking on the link in the output printed in the console."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6d583d8",
   "metadata": {},
   "source": [
    "### Online Evaluations\n",
    "\n",
    "You can use DeepEval to evaluate your LlamaIndex agents on Confident AI.\n",
    "\n",
    "1. Create a [metric collection](https://documentation.confident-ai.com/docs/llm-evaluation/metrics/create-on-the-cloud) on Confident AI.\n",
    "2. Pass the metric collection name on DeepEval's LlamaIndex agent wrapper.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07e0751b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "import asyncio\n",
    "\n",
    "from llama_index.llms.openai import OpenAI\n",
    "import llama_index.core.instrumentation as instrument\n",
    "\n",
    "import deepeval\n",
    "from deepeval.integrations.llama_index import FunctionAgent\n",
    "from deepeval.integrations.llama_index import instrument_llama_index\n",
    "\n",
    "deepeval.login(\"<your-confident-api-key>\")\n",
    "\n",
    "instrument_llama_index(instrument.get_dispatcher())\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
    "\n",
    "\n",
    "def multiply(a: float, b: float) -> float:\n",
    "    \"\"\"Useful for multiplying two numbers.\"\"\"\n",
    "    return a * b\n",
    "\n",
    "\n",
    "agent = FunctionAgent(\n",
    "    tools=[multiply],\n",
    "    llm=OpenAI(model=\"gpt-4o-mini\"),\n",
    "    system_prompt=\"You are a helpful assistant that can perform calculations.\",\n",
    "    metric_collection=\"test_collection_1\",\n",
    ")\n",
    "\n",
    "\n",
    "async def main():\n",
    "    response = await agent.run(\"What's 7 * 8?\")\n",
    "    print(response)\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    asyncio.run(main())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dda4e069",
   "metadata": {},
   "source": [
    "### References \n",
    "\n",
    "- [Get started with DeepEval](https://deepeval.com/docs/getting-started)\n",
    "- [LlamaIndex integration](https://documentation.confident-ai.com/docs/llm-tracing/integrations/llamaindex)\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
